<?php
/**
 * @author guoxiaoyong
 */
namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\OrderModel;
use bc\util\backend\Page;
use frontend\models\order\BindModel;
use bc\model\wx\report\ResultModel;

class OrderController extends BackendController
{
	
	
	public function actionIndex()
	{
		$nickname = $this->get('nickname');
		$model = new OrderModel();
		
		$params = null;
		
		if($nickname != '')
		{
			$params = "`consignee` LIKE '%{$nickname}%'";
		}
		
		$page = $this->get('page', 1);
		$list = $model->getList($params, $page, self::PAGE_SIZE);
		
		$total = $model->getTotal($params);
		
		$pagination = new Page($total, self::PAGE_SIZE);
		
		$view['list'] = $list;
		$view['pagination'] = $pagination;
		
		$view['nickname'] = $nickname;
		
		return $this->render('index', $view);
	}
	
	
	public function actionReport()
	{
		$id = $this->get('id');
		if(!$id)
			$this->redirect('/');
		
		$model = new OrderModel();
		$row = $model->getRowById($id);
		
		$bindModel = new BindModel();
		$bindModel->sort = 'bind_id ASC';
		$bindList = $bindModel->getList(['order_id' => $id]);
		
		
		$reslutModel = new ResultModel();
		$reslutModel->sort = 'report_result_id ASC';
		$reslutList = $reslutModel->getList(['status' => 1]);
		
		if(!$row)
			$this->redirect('/');
		
		$view['row'] = $row;
		$view['bind_list'] = $bindList;
		$view['result_list'] = $reslutList;
		
		return $this->render('report', $view);
	}
	
	
	public function actionSave()
	{
		$id = $this->post('bind_id');
		$model = new BindModel();
		$model->autoId = 'bind_id';
		$row = $model->getRowById($id);
		if($row['detection_time'] == 0)
		{
			$data['detection_time'] = time();
		}
		
		if($row['template_id'] != $this->post('result_id'))
		{
			$data['template_id'] = $this->post('result_id');
		}
		$model->modify($data, $id);
		echo '修改完成';
	}
}

